﻿
using GoodDotNet.Core.Data;
using GoodDotNet.Core.Entity;
using GoodDotNet.Core.IRepository;
using GoodDotNet.Core.Model;
using GoodDotNet.Core.Repository.BASE;

namespace GoodDotNet.Core.Repository
{
   public class  AdminRoleRepository :  BaseRepository< AdminRole>, AdminRoleIRepository
    {
        public  AdminRoleRepository(MyDbContext context) : base(context)
        {
        }

        /// <summary>
        /// 查询站点角色
        /// </summary>
        /// <param name="site"></param>
        /// <returns></returns>
        public List<DataGetListAdminRoleModel> GetList(int site)
        {
            var query = from role in context.AdminRole
                        join admin in context.AdminUser
                        on role.Uid equals admin.Id
                        into a
                        from b in a.DefaultIfEmpty()
                      
                        orderby role.InsertTime descending
                        where role.Deleted == 0
                        select new DataGetListAdminRoleModel
                        {
                            Id = role.Id,
                            Description = role.Description,
                            RoleName = role.RoleName,
                            Grade = role.Grade,
                            Uid = role.Uid,
                            InsertTime = role.InsertTime,
                            UpdateTime = role.UpdateTime,
                            RealName = b.RealName,
                                             
                        };
            if (site != 0)
            {
                return query.Where(a => a.Site == site).ToList();
            }
            return query.ToList();
        }

        /// <summary>
        /// 查询站点角色
        /// </summary>
        /// <param name="site"></param>
        /// <returns></returns>
        public List<DataGetListAdminRoleModel> GetUserList(int uid)
        {
            var query = from role in context.AdminRole
                        join admin in context.AdminUser
                        on role.Uid equals admin.Id
                        into a
                        from b in a.DefaultIfEmpty()
                     
                        orderby role.InsertTime descending
                        where role.Deleted == 0
                        select new DataGetListAdminRoleModel
                        {
                            Id = role.Id,
                            Description = role.Description,
                            RoleName = role.RoleName,
                            Grade = role.Grade,
                            Uid = role.Uid,
                            InsertTime = role.InsertTime,
                            UpdateTime = role.UpdateTime,
                            RealName = b.RealName,
                          
                        };
         
                return query.Where(a => a.Uid == uid).ToList();
          
        }
    }
 }
